<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="cordova.js"></script>
</head>
<body>
    <h1>hello</h1>
    <button onclick="selectpicture()">打开摄像头</button>
    <button onclick="closecamera()">关闭摄像头</button>
    <button onclick="vibrate()">开启震动</button>
    <video src="" id="video" width="300" height="240" autoplay></video>
    <script>
        document.addEventListener('deviceready',function(){
            try{
                navigator.vibrate(5000)
            }catch(e){
                document.append('<h1>'+e+'</h1>')
            }
        })
        let currentstream = null
        function selectpicture(){
            navigator.mediaDevices.getUserMedia({video:true,audio:true}).then(stream=>{
                let video = document.getElementById('video')
                video.srcObject = stream
                currentstream = stream
                video.play().catch()
            }).catch(e=>{
                alert(e)
            })
        }
        function closecamera(){
            currentstream.getTracks()[0].stop()
            currentstream.getTracks()[1].stop()
        }

        function vibrate(){
            navigator.vibrate([3000,3000])
        }

        function requestpermission(){
            var permissions = cordova.plugins.permissions;
            permissions.requestPermissions([permissions.CAMERA,permissions.RECORD_AUDIO,permissions.MODIFY_AUDIO_SETTINGS], function(e){
                alert('权限申请成功')
            }, function(e){
                alert('权限申请失败')
            });  
        }

        window.onload = requestpermission

    </script>
</body>
</html>